package com.cpi.cornpp.purchase.program.action;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchase.program.form.PurchaseProgramQueryForm;
import com.cpi.cornpp.purchase.program.services.PurchaseProgramService;
import com.cpi.cornpp.purchase.program.view.PurchaseProgramView;

/**
 * Purchase Program Delete Action
 * @author Edgar Joao
 *
 */
public class PurchaseProgramDeleteAction extends Action{


	/** Error message name*/
	private final static String ERROR_MESSAGE = "ERROR_MESSAGE";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Success forward */
	private final static String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	
	/**
	 * Purchase Program Delete
	 * @author Edgar Joao
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		PurchaseProgramService purchaseProgramService = null;
		MessageResources messageResources = null;
		String messageError = null;				
		PurchaseProgramQueryForm purchaseProgramForm = null;
		PurchaseProgramView purchaseProgramView = null;
		String forward = null;
		HttpSession httpSession = null;	
		purchaseProgramForm = (PurchaseProgramQueryForm) form;		
		String purchaseOrderId = null;
		forward = SUCCESS_FORWARD;
		try{
			
			httpSession = request.getSession(false);
			messageResources = getResources(request);
			
			if(purchaseProgramForm != null){
				purchaseProgramService = new PurchaseProgramService();
				purchaseOrderId = request.getParameter("purchaseOrderId");
				purchaseProgramView = new PurchaseProgramView();
				
				purchaseProgramView.setPurchaseOrderId(Integer.parseInt(purchaseOrderId));
				purchaseProgramService.deletePurchaseProgram(purchaseProgramView);
				purchaseProgramForm.setBiWeekInit(((Integer) httpSession.getAttribute("biWeekInit")).intValue());
				purchaseProgramForm.setBiWeekFinish(((Integer) httpSession.getAttribute("biWeekFinish")).intValue());

				messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
				httpSession.setAttribute(MESSAGE, messageError);
			}
			
		}catch (PurchaseProgramException purchaseProgramException){
			messageError = messageResources.getMessage((Locale)httpSession.getAttribute(Globals.LOCALE_KEY), purchaseProgramException.getErrorCode());
			if(null == messageError){
				httpSession.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
				form.reset(mapping, request);
				throw purchaseProgramException;
			}else{
				httpSession.setAttribute(ERROR_MESSAGE, messageError);
			}
		}
		catch (Exception e){
			PurchaseProgramException purchaseProgramException = null;
			e.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_DELETE);
			throw purchaseProgramException;
		}
		form.reset(mapping, request);				
		return mapping.findForward(forward);					
	}
	
}
